// ==UserScript==
// @name         列表跳转
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  列表iframe到详情页
// @author       JFeng
// @require      https://cdn.bootcss.com/jquery/3.4.1/jquery.js
// @match        https://sellercentral.amazon.com/merchandising-new/#default
// @match        https://sellercentral.amazon.com/merchandising-new/?*
// @icon         https://img2.baidu.com/it/u=3337194467,672949329&fm=26&fmt=auto&gp=0.jpg
// @grant        none
// ==/UserScript==

(function () {
    'use strict';

    function sleep(ms) {
        return new Promise(resolve => setTimeout(resolve, ms));
    }

    function setIframe() {
        $("#sc-content-container").after('<iframe src="" id="hieyoung20210715141801" title="AMAZON" width="33%" height="200"></iframe>');
        $("#sc-content-container").after('<iframe src="" id="hieyoung20210715141802" title="AMAZON" width="33%" height="200"></iframe>');
        $("#sc-content-container").after('<iframe src="" id="hieyoung20210715141803" title="AMAZON" width="33%" height="200"></iframe>');
    }

    function randomNum(min, max) {
        let Range = max - min;
        let Rand = Math.random();
        return min + Math.round(Rand * Range); // 四舍五入
    }

    window.onload = async function () {
        await sleep(8888)
        let url = window.location.href
        if (url.indexOf("view") === -1) {
            setIframe()
            let array = $("#smui-root > div.smui-page.manage-page-top-half.px-32.mt-26 > div.pagination-bar.d-flex.ai-center.jc-space-between.px-32.py-16.manage-footer > div > form > span").text().split(" ")
            let count = 0;

            for (let index = 0; index < array.length; index++) {
                let element = array[index]
                if (/^\d+$/.test(element)) {
                    count = element
                }
            }

            let pageNumber = parseInt($("#smui-root > div.smui-page.manage-page-top-half.px-32.mt-26 > div.pagination-bar.d-flex.ai-center.jc-space-between.px-32.py-16.manage-footer > div > form > input").attr("placeholder"))

            if (pageNumber < count) {
                let arr = $('#manage-table-container > div.manage-table > kat-table > kat-table-body > kat-table-row > kat-table-cell.manage-table-hero-cell > div > a');
                for (let index = 0; index < arr.length; index++) {
                    let element = arr[index]
                    let requestUrl = "https://sellercentral.amazon.com" + $(element).attr("href")

                    if (index % 3 === 0) {
                        $("#hieyoung20210715141801").attr("src", requestUrl)
                    } else if (index % 2 === 0) {
                        $("#hieyoung20210715141802").attr("src", requestUrl)
                    } else {
                        $("#hieyoung20210715141803").attr("src", requestUrl)
                    }
                    
                    await sleep(20000)
                }

                let indexStartNum = url.indexOf("pageNumber=")
                let start = url.substring(0, indexStartNum + 11)
                let end = url.substring(indexStartNum)
                let indexEndNum = end.indexOf("&")

                if (pageNumber + 1 <= count) {
                    window.location.href = start + (pageNumber + 1) + end.substring(indexEndNum);
                }
            }
        }
    }
})();